package com.sg.business.commons.editingsupport;

import com.sg.business.model.Deliverable;
import com.sg.business.model.Document;
import com.sg.business.model.Work;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.Widgets;
import com.sg.widgets.commons.editingsupport.CellClickListener;
import com.sg.widgets.part.CurrentAccountContext;
import com.sg.widgets.part.editor.DataObjectEditor;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
import com.sg.widgets.registry.config.DataEditorConfigurator;

public class WorkEditDocEditingSupportor extends CellClickListener {

	@Override
	protected void selected(Object element, String columnId) {
		if (element instanceof Deliverable) {
			try {
				Deliverable deliverable = (Deliverable) element;
				Document document = deliverable.getDocument();
				if (document == null) {
					return;
				}
				// ReadDocument readDocument = new ReadDocument();
				// readDocument.readDocument(document);
				Work work = deliverable.getWork();
				boolean skipPermission = work
						.canSkipDocumentPermission(new CurrentAccountContext());
				DataEditorConfigurator conf = (DataEditorConfigurator) Widgets
						.getEditorRegistry().getConfigurator(
								document.getDefaultEditorId());
				PrimaryObjectEditorInput editorInput = new PrimaryObjectEditorInput(
						document, conf, null);
				editorInput.setEnvironment("skipPermission", skipPermission);

				DataObjectEditor.open(editorInput, !Deliverable.TYPE_REFERENCE
						.equals(deliverable.getType()), true);
			} catch (Exception e) {
				MessageUtil.showToast(e);
			}
		}
	}

}
